{% extends 'dashboard/layout.html' %}

{% load currency_filters %}
{% load sorting_tags %}
{% load i18n %}

{% block title %}
    {% trans "Offers" %} | {{ block.super }}
{% endblock %}

{% block breadcrumbs %}
    <ul class="breadcrumb">
        <li>
            <a href="{{ homepage_url }}">{% trans "Dashboard" %}</a>
        </li>
        <li class="active">{% trans "Offers" %}</li>
    </ul>
{% endblock %}

{% block header %}
    <div class="page-header">
        <a href="{% url 'dashboard:offer-metadata' %}" class="btn btn-primary btn-lg pull-right"><i class="icon-plus"></i> {% trans "Create new offer" %}</a>
        <h1>{% trans "Offers" %}</h1>
    </div>
{% endblock header %}

{% block dashboard_content %}
    <div class="well">
        <form method="get" class="form-inline">
            {% include 'dashboard/partials/form_fields_inline.html' with form=form %}
            <button type="submit" class="btn btn-primary" data-loading-text="{% trans 'Searching...' %}">{% trans "Search" %}</button>
        </form>
    </div>

    <form method="post" class="order_table">
        {% csrf_token %}
        <table class="table table-striped table-bordered">
            <caption>
                <i class="icon-gift icon-large"></i>{{ queryset_description }}
                {% if is_filtered %}
                    <a href="{% url 'dashboard:offer-list' %}" >{% trans "Reset" %}</a>
                {% endif %}
            </caption>
            {% if offers %}
                <tr>
                    <th>{% anchor 'name' _('Offer name') %}</th>
                    <th>{% anchor 'start_date' _('Start date') %}</th>
                    <th>{% anchor 'end_date' _('End date') %}</th>
                    <th>{% trans "Priority" %}</th>
                    <th>{% trans "Incentive" %}</th>
                    <th>{% trans "Condition" %}</th>
                    <th>{% trans "Is available?" %}</th>
                    <th>{% trans "Restrictions" %}</th>
                    <th>{% anchor 'num_applications' _('Uses') %}</th>
                    <th>{% anchor 'total_discount' _('Cost') %}</th>
                    <th></th>
                </tr>
                {% for offer in offers %}
                    <tr>
                        <td><a href="{% url 'dashboard:offer-detail' pk=offer.pk %}">{{ offer.name }}</a></td>
                        <td>{{ offer.start_date|default:"-" }}</td>
                        <td>{{ offer.end_date|default:"-" }}</td>
                        <td>{{ offer.priority }}</td>
                        <td>{{ offer.benefit.description|safe }}</td>
                        <td>{{ offer.condition.description|safe }}</td>
                        <td>{% if offer.is_available %}
                                <span class="label label-success">{% trans "Yes" %}</span>
                            {% else %}
                                <span class="label label-danger">{% trans "No" %}</span>
                            {% endif %}
                        </td>
                        <td>
                            {% for restriction in offer.availability_restrictions %}
                                {% if not restriction.is_satisfied %}
                                    <label class="label label-danger">
                                        {{ restriction.description }}
                                    </label>
                                {% else %}
                                    {{ restriction.description }}</br>
                                {% endif %}
                            {% endfor %}
                        </td>
                        <td>{{ offer.num_applications }}</td>
                        <td>{{ offer.total_discount|currency }}</td>
                        <td>
                            <div class="btn-toolbar">
                                <div class="btn-group">
                                    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="true">
                                        {% trans "Actions" %}
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu pull-right">
                                        <li><a class="" href="{% url 'offer:detail' slug=offer.slug %}">{% trans "Browse" %}</a></li>
                                        <li><a class="" href="{% url 'dashboard:offer-detail' pk=offer.pk %}">{% trans "Stats" %}</a></li>
                                        <li><a class="" href="{% url 'dashboard:offer-metadata' pk=offer.pk %}">{% trans "Edit" %}</a></li>
                                        <li><a class="" href="{% url 'dashboard:offer-delete' pk=offer.pk %}">{% trans "Delete" %}</a></li>
                                    </ul>
                                </div>
                            </div>
                        </td>
                    </tr>
                {% endfor %}

            {% else %}
                <tr><td>{% trans "No offers found." %}</td></tr>
            {% endif %}
        </table>
        {% include "dashboard/partials/pagination.html" %}
    </form>
{% endblock dashboard_content %}
